const vscode = require('vscode');
const path = require('path');

/**
 * @param {string} type
 * @param {string} value
 * @param {string} cwd
 *
 */
function createTerminalAndExecuteNg (type, value, cwd) {
	if (value && cwd) {
		const terminal = vscode.window.createTerminal({
			name: 'Angular Tools',
			cwd: cwd,
		});
		terminal.sendText(`ng generate ${type} ${value}`, true);
	}
}

/**
 * @param {vscode.ExtensionContext} context
 */
function activate (context) {
	const iconPath = vscode.Uri.file(path.join(context.extensionPath, 'images', 'logo.png'));
	/**
	 * @type {vscode.QuickPickItem[]}
	 */
	const items = [
		{ label: 'module', description: 'Generate Angular Module', iconPath,  },
		{ label: 'component', description: 'Generate Angular Component', iconPath, },
		{ label: 'service', description: 'Generate Angular Service', iconPath, },
		{ label: 'pipe', description: 'Generate Angular Pipe', iconPath, },
		{ label: 'directive', description: 'Generate Angular Directive', iconPath, },
		{ label: 'route', description: 'Generate Angular Route', iconPath, },
		{ label: 'class', description: 'Generate Class', iconPath, },
	];

	const filesDisposable = vscode.commands.registerCommand('files', function (dir) {
		const dirPath = dir.path.substring(1);
		vscode.window.showQuickPick(items, { placeHolder: dirPath }).then(pickValue => {
			if (pickValue) {
				vscode.window.showInputBox({ placeHolder: 'Please Input Name' }).then(value => {
					createTerminalAndExecuteNg(pickValue.label, value, dir.path.substring(1));
				});
			}
		});
	});

	const templatesDisposable = vscode.commands.registerCommand('template.button', function () {});

	context.subscriptions.push(filesDisposable);
	context.subscriptions.push(templatesDisposable);
}

function deactivate () {}

module.exports = {
	activate,
	deactivate
}
